<template>
  <div>
    <el-form
      ref="ruleForm"
      class="ruleForm"
      :model="ruleForm"
      label-width="80px"
      :rules="rules"
    >
      <el-form-item label="车辆品牌" prop="nameCh">
        <el-input v-model="ruleForm.nameCh"></el-input>
      </el-form-item>
      <el-form-item label="品牌型号" prop="nameEn">
        <el-input v-model="ruleForm.nameEn"></el-input>
      </el-form-item>
      <el-form-item label="LOGO" prop="imgUrl">
        <div class="upload-img-wrap">
          <div class="upload-img">
            <img v-show="logo_current" :src="logo_current" />
          </div>
          <ul class="img-list">
            <li
              v-for="item in logo"
              :key="item.id"
              @click="logo_current = item.img"
            >
              <img
                :src="item.img"
                :alt="item.name"
                style="width: 80px; height: 50px"
              />
            </li>
          </ul>
        </div>
      </el-form-item>
      <el-form-item label="禁启用">
        <el-radio-group v-model="ruleForm.status">
          <el-radio
            v-for="item in status"
            :label="item.value"
            :value="item.value"
            :key="item.value"
            >{{ item.label }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <el-form-item label="描述">
        <el-input
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
          v-model="ruleForm.content"
        ></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import { LOGO } from "@/content/index"
  import { formRules } from "@/content/formRules"
  export default {
    props: {
      // 回显数据
      model: {
        type: Object,
        default: null,
      },
    },
    data() {
      return {
        dialogVisible: false,
        logo_current: "",
        // logo
        logo: LOGO,
        status: [
          { label: "禁用", value: false },
          { label: "启用", value: true },
        ],
        ruleForm: {
          nameCh: "",
          nameEn: "",
          imgUrl: "",
          status: true,
          content: "",
        },
        rules: formRules,
      }
    },
    watch: {
      // 监听 编辑时回显表单
      model(ruleForm) {
        this.ruleForm = { ...ruleForm } // 简单的浅克隆
      },
    },

    methods: {
      // 对外暴露获取数据的方法，内部进行表单的校验，父组件中通过refs调用
      getValue() {
        return new Promise((resolve, reject) => {
          this.ruleForm.imgUrl = this.logo_current
          this.$refs.ruleForm.validate((valid) => {
            if (valid) {
              resolve({ ...this.ruleForm })
            } else {
              reject("表单校验没通过，可以抛出一个异常")
            }
          })
        })
      },
    },
  }
</script>
<style lang="scss" scoped>
  /deep/ .el-radio__input {
    vertical-align: -2px;
  }
  .upload-img-wrap {
    display: flex;
    .upload-img {
      position: relative;
      background-color: #fbfdff;
      border: 1px dashed #c0ccda;
      border-radius: 6px;
      box-sizing: border-box;
      width: 148px;
      height: 148px;
      cursor: pointer;
      line-height: 146px;
      vertical-align: top;
      text-align: center;
      &:before {
        content: "+";
        font-size: 50px;
        font-weight: 300;
        color: #ccc;
      }
      img {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
      }
    }
    .img-list {
      flex: 1;
      padding-left: 30px;
      li {
        float: left;
        width: 60px;
        height: 60px;
        margin: 0 10px;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
</style>